﻿using Client.BaseModule.Models;
using Client.Common;
using Client.IBLL;
using Prism.Commands;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace Client.BaseModule.ViewModels {
  public class UserManagementViewModel : ViewModelBase {
    public ObservableCollection<UserModel> UserList { get; set; } = new ObservableCollection<UserModel>();
    IUserBLL _userBLL;
    IUnityContainer _unityContainer;
    IDialogService _dialogService;
    public UserManagementViewModel(IUnityContainer unityContainer, IRegionManager regionManager,
      IUserBLL userBLL, IDialogService dialogService) :base(unityContainer,regionManager) {
      PageTitle = "系统用户管理";
      _userBLL = userBLL;
      _unityContainer = unityContainer;
      _dialogService = dialogService;
    }
    public override void Refresh() {
      //用户信息刷新
      UserList.Clear();
      Task.Run(() => {
        var users = _userBLL.GetAll().GetAwaiter().GetResult();
        foreach(var item in users) {
          UserModel userModel = new UserModel {
            Index = users.IndexOf(item) + 1,
            UserId = item.UserId,
            Username = item.Username,
            UserIcon = "pack://application:,,,/Zhaoxi.SmartParking.Client.Assets;component/Images/avatar.png",
            Age = item.Age,
            Password = item.Password,
            RealName = item.RealName
          };
          //用户角色
          var roles = _userBLL.GetRolesByUserId(userModel.UserId).GetAwaiter().GetResult();
          //填充
          roles?.ForEach(r => userModel.Roles.Add(new RoleModel {
            RoleId = r.roleId,
            RoleName = r.roleName,
            State = r.state
          }));
        }
      });
    }
  }
  
    
}
